﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Threading;
using System.Collections;


namespace RemoteUser
{
    public partial class LoginForm : Form
    {
        private IPAddress serverIp;
        private int serverPort;
        
        public LoginForm()
        {
            InitializeComponent();
            //去掉顶部的三个按钮
            this.ControlBox = false;            
        }
        
        /// <summary>
        /// 检测是否输入了帐号密码，未输入给出提示,密码需要大于6位。
        /// </summary>
        /// <returns></returns>
        private bool ValidateInfo() 
        {
            if (textBoxID.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请填写用户名！",
                                "提示",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return false;
            }
            if (textBoxPwd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请填写密码！",
                                "提示",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return false;
            }
            if (textBoxPwd.Text.Length < 6)
            {
                MessageBox.Show("密码长度不足！",
                                   "提示",
                                   MessageBoxButtons.OK,
                                   MessageBoxIcon.Information);
                return false;
            }
            else
                return true;
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxID.Text = "testUser";
            textBoxPwd.Text = "123456";
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loginButton_Click(object sender, EventArgs e)
         {
            ///此函数为检测输入框是不是空
            if (!ValidateInfo())
            {
                return;
            }
            ///初始化服务器IP:PORT以供建立连接
            try
            {
                XDocument severInfo = XDocument.Load(@"C://RemoteUserConfig.xml");
                serverIp = IPAddress.Parse(severInfo.Element("PLS").Element("serverIP").Value);
                serverPort = int.Parse(severInfo.Element("PLS").Element("serverPORT").Value);             
            }
            catch
            {
                MessageBox.Show("没找到配置文件");
                return;
            }

            ///开始建立连接
            TCPConnection conn = new TCPConnection(serverIp, serverPort);
            TcpClient tcpClient = conn.Connect();
            if (tcpClient == null)
            {
                MessageBox.Show("无法连接到服务器，请重试！",
                                "错误",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }
            else
            {   /// SOCKET接收数据
                conn.myClientSocket =  tcpClient.Client;
                ///声明一个流媒体，发送数据。
                conn.netstream = tcpClient.GetStream();
                ///发送登录报文
                SendAndRecv.SendLoginRequest(conn.netstream,textBoxID.Text,textBoxPwd.Text);
                ///接收登录反馈
                int loginRespond = -1;
                XmlDocument xDocument = SendAndRecv.pullXmlFromServer(conn.myClientSocket);
                ///解析登录反馈报文
                loginRespond = TranslateXml.CheckLoginResult(xDocument);
                if (loginRespond == 0)
                {   ///登录成功，隐藏本窗口，进入PLC列表窗口
                    PLCListForm PLCListViewForm = new PLCListForm(conn.myClientSocket, conn.netstream, textBoxID.Text);
                    PLCListViewForm.Owner = this;
                    this.Hide();
                    PLCListViewForm.Show();
                }
                else
                {   ///失败则显示错误信息。此处等待完善
                    MessageBox.Show(TranslateXml.GetPLSMsg(xDocument));
                    return; 
                }
             }
         }

        private void TestButton_Click(object sender, EventArgs e)
        {
            ///此函数为检测输入框是不是空
            if (!ValidateInfo())
            {
                return;
            }
              
            serverIp = IPAddress.Parse("127.0.0.1");
            serverPort = int.Parse("3000");

            ///开始建立连接
            TCPConnection conn = new TCPConnection(serverIp, serverPort);
            TcpClient tcpClient = conn.Connect();
            if (tcpClient == null)
            {
                MessageBox.Show("无法连接到服务器，请重试！",
                                "错误",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }
            else
            {   /// SOCKET接收数据
                conn.myClientSocket = tcpClient.Client;
                ///声明一个流媒体，发送数据。
                conn.netstream = tcpClient.GetStream();
                ///发送登录报文
                SendAndRecv.SendLoginRequest(conn.netstream, textBoxID.Text, textBoxPwd.Text);
                ///接收登录反馈
                int loginRespond = -1;
                XmlDocument xDocument = SendAndRecv.pullXmlFromServer(conn.myClientSocket);
                ///解析登录反馈报文
                loginRespond = TranslateXml.CheckLoginResult(xDocument);
                if (loginRespond == 0)
                {   ///登录成功，隐藏本窗口，进入PLC列表窗口
                    PLCListForm PLCListViewForm = new PLCListForm(conn.myClientSocket, conn.netstream, textBoxID.Text);
                    PLCListViewForm.Owner = this;
                    this.Hide();
                    PLCListViewForm.Show();
                }
                else
                {   ///失败则显示错误信息。此处等待完善
                    MessageBox.Show(TranslateXml.GetPLSMsg(xDocument));
                    return;
                }
            }
        }
    }
}
